<!--
 * @Descripttion: 
 * @Autor: zhouchen
 * @Date: 2020-12-21 18:49:14
 * @LastEditTime: 2021-08-12 16:05:26
 * @LastEditTime: 2021-03-13 19:54:11

-->

<template>
  <a-card>
    <p style="font-size: 18px; text-align: center">产成品形态转换</p>
    <br />
    <a-form-model :model="queryform" v-bind="layout">
      <!-- 顶部的信息  -->

      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="单据号">
          <div>{{ queryform.document }}</div>
        </a-form-model-item>
      </a-col>

      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="单据日期">
          <div>{{ queryform.document_date }}</div>
        </a-form-model-item>
      </a-col>

      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="仓库名称">
          <div>{{ queryform.housename }}</div>
        </a-form-model-item>
      </a-col>
      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="部门">
          <div>{{ queryform.cdptid_dictText }}</div>
        </a-form-model-item>
      </a-col>
      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="业务员">
          <div>{{ queryform.cbizid_dictText }}</div>
        </a-form-model-item>
      </a-col>
      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="库存组织">
          <div>{{ queryform.pkCalbody_dictText }}</div>
        </a-form-model-item>
      </a-col>

      <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
        <a-form-model-item label="备注">
          <div>{{ queryform.remarks }}</div>
        </a-form-model-item>
      </a-col>
    </a-form-model>

    <a-table
      :columns="columns"
      :data-source="data"
      style="margin-top: 10px"
      :pagination="false"
      :scroll="autoSize"
    ></a-table>

    <div style="margin-top: 20px">
      <!-- 单据 -->
      <a-form-model :model="queryform" v-bind="layout">
        <!-- 顶部的信息 -->
        <a-row>
          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="审核人">
              <div>{{ queryform.reviewer }}</div>
            </a-form-model-item>
          </a-col>

          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="审核日期">
              <div>{{ queryform.document_date }}</div>
            </a-form-model-item>
          </a-col>

          <a-col :xl="8" :lg="8" :md="12" :sm="24" :xs="24">
            <a-form-model-item label="制单人">
              <div>{{ queryform.single_producer }}</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>

      <!-- 提交按钮组  
      1. 能审核提交的人是不能点击保存的
      2. 能保存的人是不能审核和提交的
      -->
      <div style="margin-top: 20px">
        <center>
          <!-- <a-button type="primary" style="margin-right: 20px">打印</a-button> -->

          <a-button style="margin-right: 20px" disabled>保存</a-button>

          <a-button
            @click="Examine"
            style="margin-right: 20px"
            :disabled="!alreadyCheckTag"
            >审核</a-button
          >

          <a-button
            @click="Examine_otherSide"
            style="margin-right: 20px"
            :disabled="alreadyCheckTag"
            >反审核</a-button
          >

          <!-- <a-button
            type="primary"
            @click="Submit"
            style="margin-right: 20px"
            :disabled="!alreadyRecordTag"
          >提交</a-button> -->

          <a-button type="primary" @click="save_u8c" style="margin-right: 20px">
            同步至用友
          </a-button>
        </center>
      </div>
    </div>
  </a-card>
</template>

<script>
import { XingTaiZhuanHuanLieBiao } from "@/services/modules/KuCunTiaoZheng/XingTaiZhuanHuanLieBiao.js";
const zhuanhuanliebiaohttp = new XingTaiZhuanHuanLieBiao();

import { XingTaiZhuanHuan } from "@/services/modules/KuCunTiaoZheng/XingTaiZhuanHuan.js";
const zhuanhuanhttp = new XingTaiZhuanHuan();
import axios from "axios";

import moment from "moment";
import "moment/locale/zh-cn";

const columns = [
  {
    title: "序号",
    dataIndex: "lineNo",
    key: "lineNo",
    align: "center",
    width: 200,
  },
  {
    title: "收发类别",
    dataIndex: "sarType",
    key: "sarType",
    align: "center",
    scopedSlots: { customRender: "sarType" },
    width: 200,
  },
  {
    title: "仓库名称",
    dataIndex: "housename",
    key: "housename",
    align: "center",
    width: 200,
  },
  {
    title: "类别",
    dataIndex: "type",
    key: "type",
    align: "center",
    width: 200,
    scopedSlots: { customRender: "type" },
  },
  {
    title: "存货编码",
    dataIndex: "invcode",
    key: "invcode",
    align: "center",
    scopedSlots: { customRender: "invcode" },
    width: 200,
  },
  {
    title: "存货名称",
    dataIndex: "inventoryName",
    key: "inventoryName",
    align: "center",
    width: 200,
  },
  {
    title: "规格型号",
    dataIndex: "specificationsModel",
    key: "specificationsModel",
    align: "center",
    width: 200,
  },
  {
    title: "单位",
    dataIndex: "unit",
    key: "unit",
    align: "center",
    width: 200,
  },
  {
    title: "辅助单位",
    dataIndex: "auxiliaryUnit",
    key: "auxiliaryUnit",
    align: "center",
    scopedSlots: { customRender: "auxiliaryUnit" },
    width: 200,
  },
  {
    title: "数量",
    dataIndex: "quantity",
    key: "quantity",
    align: "center",
    scopedSlots: { customRender: "quantity" },
    width: 200,
  },
  {
    title: "单价",
    dataIndex: "unitPrice",
    key: "unitPrice",
    align: "center",
    width: 200,
  },
  {
    title: "金额",
    dataIndex: "amount",
    key: "amount",
    align: "center",
    scopedSlots: { customRender: "amount" },
    width: 200,
  },
  {
    title: "批次号",
    dataIndex: "batchNo",
    key: "batchNo",
    align: "center",
    scopedSlots: { customRender: "batchNo" },
    width: 200,
  },
  {
    title: "转库日期",
    dataIndex: "transferDate",
    key: "transferDate",
    align: "center",
    scopedSlots: { customRender: "transferDate" },
    width: 200,
  },
  {
    title: "备注",
    dataIndex: "remarks",
    key: "remarks",
    align: "center",
    scopedSlots: { customRender: "remarks" },
    width: 300,
  },
  {
    title: "操作",
    dataIndex: "options",
    key: "options",
    align: "center",
    scopedSlots: { customRender: "options" },
    width: 100,
  },
];

export default {
  data() {
    return {
      u8c_array: [], //u8c传递数据表格的数据源
      arr: [], //
      arrIn: [], //其他入库单
      GeneralBillVO_46: {}, //需给用友传递的data
      GeneralBillVO_46In: [], //需给用友传递的data
      ayyanyany: [],
      ayyanyanyIn: [],
      totalData: [], //列表返回的所有数据
      updateSucessRecordTag: false, //同步至u8c按钮的状态

      // 查询表单数据
      queryform: {
        document: "",
        document_date: "",
        housename: "",
        remarks: "",
        reviewer: "",
        single_producer: "",
        single_producer_id: "",
        balance: "",
        cdptid_dictText: "",
        cbizid_dictText: "",
        pkCalbody_dictText: "",
      },

      // 格式
      layout: {
        labelCol: { span: 8 },
        wrapperCol: { span: 15 },
      },

      columns,
      data: [],
      autoSize: {
        x: 3500,
        y: 670,
      },
      alreadyCheckTag: true, // 可以审核
      alreadyRecordTag: true, // 可以记账
    };
  },
  watch: {
    $route: function () {
      this.getRouterParam();
    },
  },
  mounted() {
    this.getRouterParam();
  },

  methods: {
    //反审核函数
    Examine_otherSide() {
      let data = {
        id: this.queryform.id,
        auditMark: 2, //Y 为 1，N 为 2
      };
      // 提交审核信息，修改表一数据
      zhuanhuanhttp.postUpdateWarhouseMor(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("反审核成功");
          this.$router.push({
            path: "MorphologicalTransformationList",
          });
        } else {
          this.$message.success("反审核失败");
        }
      });
    },
    // 审核函数
    Examine() {
      this.queryform.balance = moment(this.queryform.document_date).format(
        "YYYY-MM-DD"
      );
      console.log(this.queryform.balance);
      let data = {
        id: this.queryform.id,
        auditMark: 1, //Y 为 1，N 为 2
        reviewerDate: this.queryform.balance,
      };

      // 提交审核信息，修改表一数据
      zhuanhuanhttp.postUpdateWarhouseMor(data).then((res) => {
        if (res.code === 200) {
          this.$message.success("审核成功");
          this.alreadyCheckTag = false;
        } else {
          this.$message.success("审核失败");
        }
      });
    },

    // 提交
    Submit() {
      let data = {
        id: this.queryform.id,
        bookkeepingMark: 1,
      };
      // 提交审核信息，修改表一数据
      zhuanhuanhttp.postUpdateWarhouseMor(data).then((res) => {
        if (res.code == 200) {
          this.$message.success("记账成功");
          this.alreadyRecordTag = false;
        } else {
          this.$message.success("记账失败");
        }
      });
    },

    // 单据同步至u8c
    save_u8c() {
      this.ayyanyany = [];
      this.arr = [];

      this.arrin = [];

      let _data = this.totalData;
      if (_data) {
        _data.forEach((item) => {
          if (item.transformationType_dictText == "转换前") {
            this.arr.push(item);
          } else if (item.transformationType_dictText == "转换后") {
            this.arrIn.push(item);
          }
        });
      }
      console.log("this.arr", this.arr);
      console.log("this.arrin", this.arrIn);
      this.u8c_array = [];
      // 转换前
      this.arr.forEach((item) => {
        let data = {
          cinventoryid: item.invcode, //存货id
          dshldtransnum: item.number, //应转数量
          pk_measdoc: item.shortname,
          fbillrowflag: "2", //类型
          cwarehouseid: item.storcode,
        };
        this.u8c_array.push(data);
        console.log(item);
      });
      // 转换后
      this.arrIn.forEach((item) => {
        let data = {
          cinventoryid: item.invcode, //存货id
          dshldtransnum: item.number, //应转数量
          // nadjustnum: 2,// 调整数量
          pk_measdoc: item.shortname,
          fbillrowflag: "3", //类型
          cwarehouseid: item.storcode,
        };
        this.u8c_array.push(data);
        console.log(item);
      });

      // ======= 形态转换单 ========
      this.GeneralBillVO_46 = {
        parentvo: {
          coperatorid: this.totalData[0].phone, //制单人id
          cinbsrid: this.totalData[0].workNo, //库管员ID
          dbilldate: moment(this.totalData[0].documentDate).format(
            "YYYY-MM-DD"
          ), //单据日期
          vbillcode: this.totalData[0].document, //单据号
          coutdeptid: this.totalData[0].deptcode, //部门
          pk_calbody: "100001", //库存组织编码
          pk_corp: "1001", //公司编码
          cbilltypecode: "4N",
        },
        childrenvo: this.u8c_array,
      };
      this.ayyanyany.push(this.GeneralBillVO_46);

      axios({
        headers: {
          "Access-Control-Allow-Origin": "*",
          trantype: "code",
          "Content-Type": "application/json",
          usercode: "13355262265",
          password: "5ef208ee90e5504a7b569ebbd6342634",
          system: "test",
          needStackTrace: "Y",
        },
        method: "post",
        url: "/api/u8cloud/api/ic/special4n/insertsign",
        data: {
          specialbillvo: this.ayyanyany,
        },
      }).then((response) => {
        console.log("返回的数据为::::", response.data);
        if (response.data.status == "success") {
          this.$message.success("形态转换单同步成功");
          this.updateSucessRecordTag = true;
          this.postSynchronized("已同步");
          this.$router.push("/MorphologicalTransformationList");
        } else {
          this.$message.error(response.data.errormsg);
        }
      });
      // u8c接口
      // axios({
      //   headers: {
      //     "Access-Control-Allow-Origin": "*",
      //     trantype: "code",
      //     "Content-Type": "application/json",
      //     usercode: "13355262265",
      //     password: "5ef208ee90e5504a7b569ebbd6342634",
      //     system: "test",
      //   },
      //   method: "post",
      //   url: "/proxy/u8cloud/api/ic/otherout/save/sign",
      //   data: {
      //     GeneralBillVO_4I: this.ayyanyany,
      //   },
      // }).then((response) => {
      //   console.log("返回的数据为::::", response.data);
      //   if (response.data.status == "success") {
      //     this.$message.success("同步成功");
      //     this.updateSucessRecordTag = true;
      //   } else {
      //     this.$message.error(response.data.errormsg);
      //   }
      // });

      //================== 其他入库单 ==================

      // u8c接口
      // axios({
      //   headers: {
      //     "Access-Control-Allow-Origin": "*",
      //     trantype: "code",
      //     "Content-Type": "application/json",
      //     usercode: "13355262265",
      //     password: "5ef208ee90e5504a7b569ebbd6342634",
      //     system: "test",
      //   },
      //   method: "post",
      //   url: "/proxy/u8cloud/api/ic/otherin/save/sign",
      //   data: {
      //     GeneralBillVO_4A: this.ayyanyanyIn,
      //   },
      // }).then((response) => {
      //   console.log("返回的数据为::::", response.data);
      //   if (response.data.status == "success") {
      //     this.$message.success("同步成功");
      //     this.updateSucessRecordTag = true;
      //   } else {
      //     this.$message.error(response.data.errormsg);
      //   }
      // });
    },
    //是否同步标志位
    postSynchronized(isOrNo) {
      let data = {
        id: this.queryform.id,
        isSynchronized: isOrNo,
      };
      zhuanhuanhttp.postUpdateWarhouseMor(data).then((res) => {
        console.log(res);
      });
    },
    // 获取路由参数
    getRouterParam() {
      console.log("查询参数:::", this.$route.query.Id);
      // 通过出入库id 查询出入库信息
      if (typeof this.$route.query.Id == "undefined") {
        console.log("常规跳转");
      } else {
        this.setInitInfo(this.$route.query.Id);
      }
    },

    // 根据路由传递id初始化表单信息
    setInitInfo(id) {
      this.alreadyCheckTag = true;
      let exa; // 是否可点击审核
      let sub; // 是否可点击提交
      zhuanhuanliebiaohttp
        .getWarhouseMorphologicalTransformationList(
          `?id=${id}&pageSize=200&pageNo=1`
        )
        .then((res) => {
          console.log("----- routerListRes -----", res);
          let getData = res.result.records[0];
          exa = getData.auditMark;
          sub = getData.bookkeepingMark;
          this.queryform.id = getData.id;
          this.queryform.document = getData.document;
          this.queryform.document_date = getData.documentDate;
          this.queryform.housename = getData.depotId_dictText;
          this.queryform.remarks = getData.headRemark;
          this.queryform.reviewer = getData.reviewer_dictText;
          this.queryform.single_producer = getData.preparedBy_dictText;
          this.queryform.single_producer_id = getData.preparedBy;
          this.queryform.cdptid_dictText = getData.cdptid_dictText; //部门
          this.queryform.cbizid_dictText = getData.cbizid_dictText; //业务员
          this.queryform.pkCalbody_dictText = getData.pkCalbody_dictText; //库存组织
          // 将数据赋值给表格
          let array = res.result.records;
          let resArray = [];
          for (let i = 0; i < array.length; i++) {
            let singleData = {
              key: i + 1,
              lineNo: i + 1,
              id: array[i].twoId, // id
              sarType: array[i].sendAndReceiveCategory_dictText, // 收发类别
              type: array[i].transformationType_dictText, // 转换类别
              inventoryId: array[i].inventoryId, // 存货id
              invcode: array[i].invcode, // 存货编码
              inventoryName: array[i].inventoryId_dictText, // 存货名称
              specificationsModel: array[i].specificationsModel, // 规格型号
              auxiliaryUnit: array[i].auxiliaryUnit_dictText, // 辅助单位
              unit: array[i].unit, //单位
              quantity: array[i].number, // 数量
              amount: array[i].money, // 金额
              hourseId: array[i].depotId, // 仓库id
              housename: array[i].depotId_dictText, // 仓库名称
              batchNo: array[i].batch, // 批次号
              transferDate: array[i].documentDate, // 转库日期
              remarks: array[i].remark, // 备注
            };
            resArray.push(singleData);
          }
          if (exa === 1) {
            this.alreadyCheckTag = false;
          }
          if (sub === 1) {
            this.alreadyRecordTag = false;
          }
          this.data = resArray;
          this.totalData = res.result.records;
          console.log("this.totalData", this.totalData);
        });
    },
  },
};
</script>

<style>
</style>
